// releasePage/pages/homePageInfo/brashCard/editBrashCard/editBrashCard.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        ipxBottom: 18,
        current: '00', //设备类型
        ipxBottom: 18,
        index: 0,
        companyContent: [],
        showCover: false,
        inputValue: '',
        id: '', //设备厂商id
        companyDescription: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            current: options.current,
        });
        this.getNfcDeviceList();
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 52,
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    refresh() {
        let that = this;
        that.setData({});
        that.getNfcDeviceList();
    },
    getCompany(e) {
        this.setData({
            inputValue: e.detail.value,
        });
    },
    companyDescription(e) {
        this.setData({
            companyDescription: e.detail.value,
        });
    },
    //选择设备厂商
    chooseScene(e) {
        this.setData({
            showCover: true,
            inputValue: e.currentTarget.dataset.scene,
            index: e.currentTarget.dataset.index,
            id: e.currentTarget.dataset.id,
            companyDescription: e.currentTarget.dataset.scenedesc,
        });
    },
    //弹窗
    //编辑设备厂商
    addScene(e) {
        let _that = this;
        if (!_that.data.inputValue) {
            wx.showToast({
                title: '请填写设备名称',
                icon: 'none',
                duration: 2000,
            });
            return;
        }
        //编辑
        let arr = 'companyContent[' + _that.data.index + '].devicemanufacturer';
        _that.setData({
            [arr]: _that.data.inputValue,
            showCover: false,
        });
        wx.request({
            url:
                app.globalData.rootPath +
                `cjMap/editNfcDeviceManufacturer?authId=${app.globalData.authId}`,
            method: 'POST',
            header: {
                'content-type': 'application/json',
            },
            data: {
                delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
                devicemanufacturer: _that.data.inputValue,
                devicemanufacturerdesc: _that.data.companyDescription,
                id: _that.data.id,
            },
            success: function (res) {
                console.log(res);
                if (res.data.status == 0) {
                    wx.showToast({
                        title: 'OK',
                        icon: 'success',
                        duration: 2000,
                    });
                } else {
                    wx.showToast({
                        title: '提交失败，请重新录入或联系后台管理',
                        icon: 'none',
                        duration: 2000,
                    });
                }
                _that.setData({
                    companyContent: [],
                    inputValue: '',
                });
                _that.getNfcDeviceList();
                let pages = getCurrentPages();
                let prevPage = pages[pages.length - 2];
                prevPage.refresh();
            },
        });
    },
    //删除设备厂商
    deleteScene(e) {
        let _that = this;
        wx.request({
            url:
                app.globalData.rootPath +
                `cjMap/getNfcRelativeCount?authId=${app.globalData.authId}&id=${e.currentTarget.dataset.id}`,
            method: 'GET',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                console.log(res.data.count);
                if (res.data.count) {
                    wx.navigateTo({
                        url: `../deleteBrashCard/deleteBrashCard?id=${e.currentTarget.dataset.id}&name=${e.currentTarget.dataset.name}&num=${res.data.count}&current=${_that.data.current}`,
                    });
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '确认删除？',
                        success: function (res) {
                            if (res.confirm) {
                                wx.request({
                                    url:
                                        app.globalData.rootPath +
                                        `cjMap/deleteNfcDeviceManufacturer?authId=${app.globalData.authId}&id=${e.currentTarget.dataset.id}`,
                                    method: 'GET',
                                    header: {
                                        'content-type': 'application/json',
                                    },
                                    success: function (res) {
                                        console.log(res);
                                        _that.setData({
                                            companyContent: [],
                                            inputValue: '',
                                        });
                                        let pages = getCurrentPages();
                                        let prevPage = pages[pages.length - 2];
                                        prevPage.refresh();
                                        _that.getNfcDeviceList();
                                    },
                                });
                            } else {
                                console.log('点击取消回调');
                            }
                        },
                    });
                }
            },
        });
    },
    //弹窗
    hideCover(e) {
        this.setData({
            showCover: false,
        });
    },
    stopEvent(e) {
        return;
    },
    //保存
    comeBack(e) {
        wx.navigateBack({
            delta: 1, // 返回上一级页面。
        });
    },
    // 获取网络场景列表
    getNfcDeviceList() {
        let _that = this;
        wx.request({
            url:
                app.globalData.rootPath +
                `cjMap/getNfcDeviceManufacturerList?authId=${app.globalData.authId}&createtimeorder=asc`,
            method: 'GET',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                console.log(res);
                _that.setData({
                    companyContent: res.data.data,
                });
            },
        });
    },
});
